package com.leoman.spinningmachine.tab1;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.leoman.helper.util.RefreshUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.LeaveGroup;
import com.leoman.spinningmachine.bean.QyNoticeBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.BGARefreshUtil;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 企业通知
 */
public class QytzActivity extends BaseActivity {

    @BindView(R.id.bgaRefreshLayout)
    BGARefreshLayout bgaRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView recyclerView;
    private List<LeaveGroup> list = new ArrayList<>();
    private QytzAdapter adapter;
    private List<QyNoticeBean> qyNoticeList = new ArrayList<>();
    private int page;
    private String token;
    private boolean isLastPage;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_qytz;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("邵纺机");
        adapter = new QytzAdapter(this,R.layout.item_qytz,null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        token = SharedPreferencesUtils.getInstance().getString("token");
        page = 1;
        bgaRefreshLayout.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(this,bgaRefreshLayout);
        bgaRefreshLayout.beginRefreshing();
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        super.OnSuccessData(apiResponse, tag);
        BGARefreshUtil.endLoading(tag,bgaRefreshLayout);
        isLastPage = BGARefreshUtil.getIsLastPage(apiResponse);
        if(page == 1){
            qyNoticeList.clear();
        }
        qyNoticeList.addAll(apiResponse.data.list);
//            qyNoticeList = apiResponse.data.list;
        adapter.setData(qyNoticeList);
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        super.OnErrorData(code, tag);
        bgaRefreshLayout.endRefreshing();
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error, tag);
        bgaRefreshLayout.endRefreshing();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(refreshLayout);
        page = 1;
        WebServiceApi.getInstance().qynoticeList(token,""+page, ""+Constant.pageSize,this,1);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if(isLastPage){
            return false;
        }
        page++;
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().qynoticeList(token,""+page, ""+Constant.pageSize,this,2);
        return true;
    }
}
